package com.kcht.rfid.rd30.secretary.dispatch;

import com.kcht.rfid.rd30.secretary.core.command.ReaderCommand;
import com.kcht.rfid.rd30.secretary.core.common.Rd30Configuration;

import java.util.Map;
import java.util.Observable;
import java.util.Observer;

/**
 * 命令执行
 * */
public abstract class BaseCommandExecute extends Observable {
    /**频点 920125~924875,每250一个频点*/
    private final String FREQ="freq";
    /**功率 */
    private final String POWER="power";
    /**0-3(FM0,M2,M4,M8),纠错级别*/
    private final String CODE_TYPE="code_type";
    /**阅读器发射速率40 80 160*/
    private final String FORWARD_RATE="forward_rate";
    /**标签返回速率40 80 160 320*/
    private final String REVERSE_RATE="reverse_rate";
    /**天线号1-4*/
    private final String ANTS="ants";
    protected Rd30Configuration configuration=new Rd30Configuration();

    /**
     * 设置初始化参数
     * */
    public BaseCommandExecute(Map<String, String> params) {
        paramsVerify(params);
    }

    /**
     * 命令执行
     * */
    protected abstract void execute(IReaderChannel channel, ReaderCommand cmd, Observer[] observers);

    /**
     * 中断命令
     * */
    protected abstract void interrupt();

    /**
     * 参数校验
     * todo 校验参数
     * */
    private void paramsVerify(Map<String, String> params){
        String[] freqStrArray = params.get(FREQ).split(",");
        int[] freqs=new int[freqStrArray.length];
        for (int i = 0; i < freqs.length; i++) {
            freqs[i]=Integer.parseInt(freqStrArray[i]);
        }
        configuration.setFreqs(freqs);
        configuration.setPower(Integer.parseInt(params.get(POWER)));
        configuration.setCodeType(Integer.parseInt(params.get(CODE_TYPE)));
        configuration.setForwardRate(Integer.parseInt(params.get(FORWARD_RATE)));
        configuration.setReverseRate(Integer.parseInt(params.get(REVERSE_RATE)));
        String[] antStrArray = params.get(ANTS).split(",");
        int[] ants=new int[antStrArray.length];
        for (int i = 0; i < ants.length; i++) {
            ants[i]=Integer.parseInt(antStrArray[i]);
        }
        configuration.setAnts(ants);
    }
}
